﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Collections;

namespace Wydatki
{
    public partial class Form2 : ProgNetComponentsDx.Forms.HideToTrayForm
    {
        public Form2()
        {
            InitializeComponent();
        }
        private List<string> list = new List<string>();
        private BindingList<string> bindingList;    
        Random r = new Random();
            
        private void Form2_Load(object sender, EventArgs e)
        {
            bindingList = new BindingList<string>(list);
            tabControl1.AllowDrop = true;

            comboBox1.DataSource = bindingList;
        }

        DragForm f = new DragForm("");
        
        private Point GetDragFormLocation()
        {
            Point pt = new Point(Control.MousePosition.X+10, Control.MousePosition.Y+6);
            return pt;
        }
        
        private void tabControl1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button != System.Windows.Forms.MouseButtons.Left)
                return;
            TabPage dragPage = null;
            TabControl tb = (sender as TabControl);
            if (tb == null)
                return;
            for (int i=0; i < tb.TabPages.Count; i++)
            {
                if (tb.GetTabRect(i).Contains(e.Location))
                {
                    dragPage = tb.TabPages[i];
                    f.Text = dragPage.Text;
                    f.Location = GetDragFormLocation();
                    tb.DoDragDrop(dragPage, DragDropEffects.Move);
                    break;
                }
            }
        }

        private void tabControl1_DragOver(object sender, DragEventArgs e)
        {
            TabControl tb = (sender as TabControl);
            if (tb == null)
                return;
            Point pt = GetDragFormLocation();
            if (f != null)
            {
                if (!f.Visible)
                    f.Show();
                f.Location = pt;
            }
            if (e.Data.GetDataPresent(typeof(TabPage)))
            {
                e.Effect = DragDropEffects.Move;
                TabPage dragPage = (TabPage)e.Data.GetData(typeof(TabPage));
                if (!tb.TabPages.Contains(dragPage))
                {
                    e.Effect = DragDropEffects.None;
                    return;
                }
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        private void tabControl1_DragDrop(object sender, DragEventArgs e)
        {
            TabControl tb = (sender as TabControl);
            if (tb == null)
                return;

            if (f != null)
            {
                f.Hide();
            }

            TabPage dropPage = null;

            if (e.Data.GetDataPresent(typeof(TabPage)))
            {
                e.Effect = DragDropEffects.Move;
                TabPage dragPage = (TabPage)e.Data.GetData(typeof(TabPage));
                if (!tb.TabPages.Contains(dragPage))
                {
                    e.Effect = DragDropEffects.None;
                    return;
                }
                Point pt = tb.PointToClient(new Point(e.X, e.Y));
                for (int i=0; i < tb.TabPages.Count; i++)
                {
                    if (tb.GetTabRect(i).Contains(pt))
                    {
                        dropPage = tb.TabPages[i];
                        break;
                    }
                }
                if (dropPage != null && dropPage != dragPage)
                {
                    int dragIndex = tb.TabPages.IndexOf(dragPage);
                    int dropIndex = tb.TabPages.IndexOf(dropPage);
                    if (dropIndex != dragIndex)
                    {
                        tb.SuspendLayout();
                        if (dragIndex > dropIndex) //<------
                        {
                            for (int i=dragIndex; i > dropIndex; i--)
                            {
                                dropPage = tb.TabPages[i - 1];
                                tb.TabPages.Remove(dropPage);
                                tb.TabPages.Insert(i, dropPage);
                            }
                        }
                        else //----->
                        {
                            for (int i=dragIndex; i < dropIndex; i++)
                            {
                                dropPage = tb.TabPages[i + 1];
                                tb.TabPages.Remove(dropPage);
                                tb.TabPages.Insert(i, dropPage);
                            }
                        }
                        tb.ResumeLayout(false);
                        tb.SelectedTab = dragPage;
                    }
                }
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        private void tabControl1_DragEnter(object sender, DragEventArgs e)
        {
            if (f != null)
            {
                TabPage dragPage = (TabPage)e.Data.GetData(typeof(TabPage));
                if (!(sender as TabControl).TabPages.Contains(dragPage))
                {
                    e.Effect = DragDropEffects.None;
                    return;
                }
                else
                {
                    e.Effect = DragDropEffects.Move;
                }
                f.Location = GetDragFormLocation();
                f.Show();
            }
        }

        private void tabControl1_DragLeave(object sender, EventArgs e)
        {
            f.Hide();
        }
        
        private class DragForm : Form
        {
            private Label l;
            public DragForm(string text)
            {
                InitializeComponent();
                Text = text;
            }

            private void InitializeComponent()
            {
                l = new Label();
                l.AutoSize = false;
                l.Dock = DockStyle.Fill;
                l.TextAlign = ContentAlignment.MiddleCenter;
                this.Controls.Add(l);
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                this.ShowInTaskbar = false;
                this.StartPosition = FormStartPosition.Manual;
                this.Opacity = 0.8D;
                this.AllowDrop = false;
            }

            public override string Text
            {
                get
                {
                    if (l != null)
                        return l.Text;
                    return base.Text;
                }
                set
                {
                    if (l != null)
                        l.Text = value;
                    else 
                        base.Text = value;
                    Size s  = System.Windows.Forms.TextRenderer.MeasureText(value, (l != null ? l.Font : this.Font));
                    this.Size = new Size(s.Width+10, s.Height+10);
                }
            }
            protected override void OnLoad(EventArgs e)
            {
                Text = Text;
            }
        }
       
        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            f.Close();
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            this.HideToTray();
        }

        private void layoutControl_Changed(object sender, EventArgs e)
        {
            
        }
    }
 
}
